package com.hs.security.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.security.exception.ProjectException;
import com.hs.security.mapper.TbRoleMapper;
import com.hs.security.mapper.TbRolePermissionMapper;
import com.hs.security.pojo.TbRole;
import com.hs.security.service.TbRoleService;
import com.hs.security.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 郭敖
 * @since 2023-08-18
 */
@Service
public class TbRoleServiceImpl extends ServiceImpl<TbRoleMapper, TbRole> implements TbRoleService {

    @Autowired
    private TbRoleMapper mapper;
    @Autowired
    private TbRolePermissionMapper rolePermissionMapper;

    @Override
    public Page<TbRole> findRoles(Integer pageNum, Integer pageSize) {
        Page<TbRole> page = new Page<>(pageNum,pageSize);
        return mapper.selectPage(page, null);
    }

    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public int updateRolePermission(TbRole role) {
        int delete = rolePermissionMapper.deleteByRoleIdInt(role.getId());
        int insert = rolePermissionMapper.insertByRoleIds(role);
        if(insert < 1){
            throw new ProjectException(Constant.CREATED_ERR.getMsg(),Constant.CREATED_ERR.getCode());
        }
        return insert;
    }
}
